package com.ng.desktop.expansionsa.view.articulo;

import javax.swing.JOptionPane;

import com.ng.desktop.expansionsa.model.Articulo;
import com.ng.desktop.expansionsa.persistence.ArticuloPersistence;
import com.ng.framework.view.desktop.NGTablePanel;

public class ArticuloTablePanel extends NGTablePanel<Articulo, ArticuloPersistence> {

	private static final long serialVersionUID = 3723967760576931024L;

	private static final String[] COLUMN_NAMES = new String[]{"Id", "Categoría", "Descripción", "Pcio. x día", "Pcio. x semana",
			"Pcio. x quincena", "Pcio. x mes"};

	private static final int[] COLUMN_WIDTHS = {50, 140, 140, 95, 95, 95, 95};

	private FilterCategoria filterCategoria;

	public ArticuloTablePanel(ArticuloPersistence persistence, FilterCategoria filterCategoria) {
		super(persistence);

		this.filterCategoria = filterCategoria;
	}

	@Override
	protected Object[] toRow(Articulo dataObject) {
		return new Object[]{dataObject.getId(), dataObject.getCategoria(),
				dataObject.getDescripcion() == null ? "" : dataObject.getDescripcion(), dataObject.getPrecioDia(),
				dataObject.getPrecioSemana(), dataObject.getPrecioQuincena(), dataObject.getPrecioMes()};
	}

	@Override
	protected String[] getColumnNames() {
		return COLUMN_NAMES;
	}

	@Override
	protected int[] getColumnWidths() {
		return COLUMN_WIDTHS;
	}

	@Override
	protected boolean edit(ArticuloPersistence persistence, Articulo instance) {
		ArticuloEditDialog dialog = new ArticuloEditDialog(JOptionPane.getFrameForComponent(this), persistence, instance);
		dialog.setLocationRelativeTo(this);
		dialog.setVisible(true);

		this.filterCategoria.actualizarCategorias();

		return true;
	}

	@Override
	protected Articulo getSelectedInstance(ArticuloPersistence persistence, Object[] rowData) {
		return persistence.findById((Long) rowData[0]);
	}

	@Override
	protected boolean create(ArticuloPersistence persistence) {
		ArticuloEditDialog dialog = new ArticuloEditDialog(JOptionPane.getFrameForComponent(this), persistence);
		dialog.setLocationRelativeTo(this);
		dialog.setVisible(true);

		this.filterCategoria.actualizarCategorias();

		return true;
	}

}
